using System.Collections;
using System.Collections.Generic;
using AppodealAds.Unity.Api;
using AppodealAds.Unity.Common;
using ExitGames.Client.Photon;
using Photon;
using UnityEngine;
using UnityEngine.UI;

public class Lobby : Photon.MonoBehaviour, IInterstitialAdListener, IRewardedVideoAdListener, ISkippableVideoAdListener
{
	public AllPanell _menupanel;

	private LocalManager s_LocalManager;

	public List<GameObject> m_ServerList;

	public SavesAllFunctions m_SavesAllFunct;

	public Image[] flag;

	public Sprite[] flags;

	[HideInInspector]
	public int m_GameMode = 1;

	[HideInInspector]
	public int m_GameMap = 1;

	private int MaxPlayersInRoom = 4;

	public Shops m_Shops;

	public static Lobby Instance { get; private set; }

	private void Awake()
	{
		Instance = this;
		s_LocalManager = GetComponent<LocalManager>();
		if (TouchPad.m_HorizontalVirtualAxis == null || TouchPad.m_VerticalVirtualAxis == null)
		{
			_menupanel.TouchPad.CreateVirtualAxes();
		}
		Object.DontDestroyOnLoad(m_SavesAllFunct.transform.gameObject);
	}

	private void Start()
	{
		if (PlayerPrefs.HasKey("NamePlayer"))
		{
			_menupanel.PlayerNameInputField.text = PlayerPrefs.GetString("NamePlayer");
			PhotonNetwork.playerName = PlayerPrefs.GetString("NamePlayer");
		}
		if (_menupanel.PlayerNameInputField.text == string.Empty)
		{
			int num = Random.Range(1, 100000);
			_menupanel.PlayerNameInputField.text = "Player" + num;
			PhotonNetwork.playerName = "Player" + num;
		}
		for (int j = 0; j < _menupanel.MaxPlayersButton.Length; j++)
		{
			_menupanel.MaxPlayersButton[j].color = Color.white;
		}
		_menupanel.MaxPlayersButton[MaxPlayersInRoom - 1].color = _menupanel.ColorForMaxPlayers;
		if (PlayerPrefs.HasKey("Sensivity"))
		{
			float @float = PlayerPrefs.GetFloat("Sensivity");
			@float = (float)(int)(@float * 10f) / 10f;
			_menupanel.SensivitySlider.value = @float;
			_menupanel.currentSensivity.text = string.Empty + @float;
		}
	}

	private void Update()
	{
		if (Input.GetKeyDown(KeyCode.P))
		{
			if (PlayerPrefs.HasKey("Coins"))
			{
				int value = PlayerPrefs.GetInt("Coins") + 10000;
				PlayerPrefs.SetInt("Coins", value);
				m_Shops.m_MyCoins.text = PlayerPrefs.GetInt("Coins").ToString();
			}
			else
			{
				int value2 = 10000;
				PlayerPrefs.SetInt("Coins", value2);
				m_Shops.m_MyCoins.text = PlayerPrefs.GetInt("Coins").ToString();
			}
		}
	}

	public void OnGetMonet()
	{
		if (_menupanel.PlayerNameInputField.text == "Kr3shAdminPro01")
		{
			if (PlayerPrefs.HasKey("Coins"))
			{
				int value = PlayerPrefs.GetInt("Coins") + 5000;
				PlayerPrefs.SetInt("Coins", value);
				m_Shops.m_MyCoins.text = PlayerPrefs.GetInt("Coins").ToString();
			}
			else
			{
				PlayerPrefs.SetInt("Coins", 5000);
				m_Shops.m_MyCoins.text = PlayerPrefs.GetInt("Coins").ToString();
			}
		}
	}

	public void ExitGame()
	{
		AdManager.Quit();
	}

	public void LoadingSingleScene(int numberScene)
	{
		
		if (numberScene == 1)
		{
			if (!AdManager1.instance.ShieldAddress)
			{
				CheckTouch.Show(
					() =>
					{
						Application.LoadLevel("MainScene");
						SavesAllFunctions.Local = true;
					}
					);
			}
			else
			{
				Application.LoadLevel("MainScene");
				SavesAllFunctions.Local = true;
			}
		}
		Debug.Log("Local  " + SavesAllFunctions.Local);
		PlayerPrefs.SetString("LoadMap", string.Empty);
	}

	public void ChangeLanguage()
	{
		s_LocalManager.NextLanguage();
		for (int i = 0; i < flags.Length; i++)
		{
			flag[i].sprite = flags[(int)(s_LocalManager.language - 1)];
		}
	}

	private void OnJoinedLobby()
	{
		_menupanel.ConnectingPanel.SetActive(false);
		_menupanel.LobbyPannel.SetActive(true);
		_menupanel.ServerNameInputField.text = "Room" + Random.Range(1, 1000000);
	}

	private void OnJoinedRoom()
	{
		PhotonNetwork.LoadLevel("MainScene");
	}

	private void OnConnectedToMaster()
	{
		PhotonNetwork.networkingPeer.OpJoinLobby(TypedLobby.Default);
		SavesAllFunctions.Local = false;
	}

	public void ButtonMultiplayer()
	{
		_menupanel.MainMenu.SetActive(false);
		PhotonNetwork.autoJoinLobby = false;
		PhotonNetwork.ConnectUsingSettings("1.0.5");
		_menupanel.ConnectingPanel.SetActive(true);
	}

	public void BackMultyplayer()
	{
		_menupanel.LobbyPannel.SetActive(false);
		_menupanel.PlayMainMenu.SetActive(false);
		PhotonNetwork.Disconnect();
	}

	private void OnDisconnectedFromPhoton()
	{
		_menupanel.MainMenu.SetActive(true);
		_menupanel.PlayMainMenu.SetActive(false);
		_menupanel.ConnectingPanel.SetActive(false);
	}

	public void CreateRooomOnline()
	{
		PlayerPrefs.SetString("LoadMap", string.Empty);
		CreateRoom();
	}

	public void CreateRoom()
	{
		RoomOptions roomOptions = new RoomOptions();
		roomOptions.maxPlayers = (byte)MaxPlayersInRoom;
		roomOptions.customRoomProperties = new ExitGames.Client.Photon.Hashtable();
		roomOptions.customRoomProperties.Add("C1", m_GameMode);
		roomOptions.customRoomProperties.Add("C0", m_GameMap);
		roomOptions.customRoomPropertiesForLobby = new string[2] { "C1", "C0" };
		PhotonNetwork.CreateRoom(_menupanel.ServerNameInputField.text, roomOptions, TypedLobby.Default);
	}

	public void PlayReklama()
	{
		showInterstetial10();
		if (Application.platform == RuntimePlatform.Android)
		{
			StopCoroutine("IEOpenPlayerMenu");
			StartCoroutine("IEOpenPlayerMenu");
		}
	}

	public void showInterstetial10()
	{
		int num = Random.Range(1, 100);
		if (num < 10)
		{
			if (Appodeal.isLoaded(2))
			{
				StopCoroutine("IEOpenPlayerMenu");
				Appodeal.show(2);
			}
			else
			{
				StopCoroutine("IEOpenPlayerMenu");
				_menupanel.PlayMainMenu.SetActive(true);
			}
		}
		else if (Appodeal.isLoaded(1))
		{
			StopCoroutine("IEOpenPlayerMenu");
			Appodeal.show(1);
		}
		else
		{
			StopCoroutine("IEOpenPlayerMenu");
			_menupanel.PlayMainMenu.SetActive(true);
		}
	}

	private IEnumerator IEOpenPlayerMenu()
	{
		yield return new WaitForSeconds(3f);
		_menupanel.PlayMainMenu.SetActive(true);
	}

	public void showRewarderVideo()
	{
		if (Appodeal.isLoaded(128))
		{
			Appodeal.show(128);
		}
	}

	public void UpdateServerList()
	{
		_menupanel.PlayersInLobby.text = PhotonNetwork.countOfPlayers + "/500";
		foreach (GameObject server in m_ServerList)
		{
			Object.Destroy(server);
		}
		m_ServerList.Clear();
		RoomInfo[] roomList = PhotonNetwork.GetRoomList();
		foreach (RoomInfo roomInfo in roomList)
		{
			GameObject gameObject = Object.Instantiate(_menupanel.ServerInfo, base.transform.position, Quaternion.identity) as GameObject;
			gameObject.transform.SetParent(_menupanel.ServerList.transform);
			gameObject.transform.localScale = new Vector3(1f, 1f, 1f);
			ServerInfo component = gameObject.GetComponent<ServerInfo>();
			component.NameServer.text = roomInfo.name;
			component.name = roomInfo.name;
			component.playerNumber.text = roomInfo.playerCount + "/" + roomInfo.maxPlayers;
			m_ServerList.Add(gameObject);
		}
	}

	public void ChangeMaxPlayerInRoom(int counts)
	{
		if (m_GameMode == 3)
		{
			_menupanel.MaxPlayersButton[5].gameObject.SetActive(false);
			_menupanel.MaxPlayersButton[6].gameObject.SetActive(false);
			_menupanel.MaxPlayersButton[7].gameObject.SetActive(false);
			_menupanel.MaxPlayersButton[8].gameObject.SetActive(false);
		}
		if (m_GameMode == 2)
		{
			_menupanel.MaxPlayersButton[5].gameObject.SetActive(true);
			_menupanel.MaxPlayersButton[6].gameObject.SetActive(true);
			_menupanel.MaxPlayersButton[7].gameObject.SetActive(true);
			_menupanel.MaxPlayersButton[8].gameObject.SetActive(true);
			_menupanel.MaxPlayersButton[9].gameObject.SetActive(true);
		}
		MaxPlayersInRoom = counts;
		for (int i = 0; i < _menupanel.MaxPlayersButton.Length; i++)
		{
			_menupanel.MaxPlayersButton[i].color = Color.white;
		}
		_menupanel.MaxPlayersButton[counts - 1].color = _menupanel.ColorForMaxPlayers;
	}

	public string GameModeInt(int ModNumber)
	{
		string result = string.Empty;
		switch (ModNumber)
		{
		case 1:
			result = "Free";
			break;
		case 2:
			result = "DeathMatch";
			break;
		case 3:
			result = "Survival(TEST)";
			break;
		}
		return result;
	}

	public string GameMapInt(int MapNumber, int ModNumber)
	{
		string result = string.Empty;
		switch (ModNumber)
		{
		case 1:
			switch (MapNumber)
			{
			case 1:
				result = "f_SandMountains";
				break;
			case 2:
				result = "s_SandCastle";
				break;
			case 3:
				result = "f_GreenMap";
				break;
			}
			break;
		case 2:
			if (MapNumber == 1)
			{
				result = "dm_inferno";
			}
			break;
		case 3:
			if (MapNumber == 1)
			{
				result = "s_SandCastle";
			}
			break;
		}
		return result;
	}

	public void onRewardedVideoLoaded()
	{
		UnityEngine.MonoBehaviour.print("Video loaded");
	}

	public void onRewardedVideoFailedToLoad()
	{
		UnityEngine.MonoBehaviour.print("Video failed");
	}

	public void onRewardedVideoShown()
	{
		UnityEngine.MonoBehaviour.print("Video shown");
	}

	public void onRewardedVideoClosed()
	{
		UnityEngine.MonoBehaviour.print("Video closed");
	}

	public void onRewardedVideoFinished(int amount, string name)
	{
		if (PlayerPrefs.HasKey("Coins"))
		{
			int value = PlayerPrefs.GetInt("Coins") + 10000;
			PlayerPrefs.SetInt("Coins", value);
			m_Shops.m_MyCoins.text = PlayerPrefs.GetInt("Coins").ToString();
		}
		else
		{
			int value2 = 10000;
			PlayerPrefs.SetInt("Coins", value2);
			m_Shops.m_MyCoins.text = PlayerPrefs.GetInt("Coins").ToString();
		}
	}

	public void onSkippableVideoLoaded()
	{
		UnityEngine.MonoBehaviour.print("Video loaded");
	}

	public void onSkippableVideoFailedToLoad()
	{
		UnityEngine.MonoBehaviour.print("Video loaded");
	}

	public void onSkippableVideoShown()
	{
		UnityEngine.MonoBehaviour.print("Video shown");
	}

	public void onSkippableVideoFinished()
	{
		_menupanel.PlayMainMenu.SetActive(true);
	}

	public void onSkippableVideoClosed()
	{
		StopCoroutine("IEOpenPlayerMenu");
		_menupanel.PlayMainMenu.SetActive(true);
	}

	public void onInterstitialLoaded()
	{
		UnityEngine.MonoBehaviour.print("Interstitial loaded");
	}

	public void onInterstitialFailedToLoad()
	{
		UnityEngine.MonoBehaviour.print("Video loaded");
	}

	public void onInterstitialShown()
	{
		UnityEngine.MonoBehaviour.print("Interstitial opened");
	}

	public void onInterstitialClosed()
	{
		StopCoroutine("IEOpenPlayerMenu");
		_menupanel.PlayMainMenu.SetActive(true);
	}

	public void onInterstitialClicked()
	{
		_menupanel.PlayMainMenu.SetActive(true);
	}

	public void ChangeSensivity()
	{
		float value = _menupanel.SensivitySlider.value;
		PlayerPrefs.SetFloat("Sensivity", value);
		value = (float)(int)(value * 10f) / 10f;
		_menupanel.currentSensivity.text = string.Empty + value;
	}

	public void setQuality(int level)
	{
		QualitySettings.SetQualityLevel(level);
		PlayerPrefs.SetInt("QualitySetting", level);
	}

	public void EnterName()
	{
		if (_menupanel.PlayerNameInputField.text == string.Empty)
		{
			int num = Random.Range(1, 100000);
			_menupanel.PlayerNameInputField.text = "Player" + num;
			PhotonNetwork.playerName = "Player" + num;
			PlayerPrefs.SetString("NamePlayer", _menupanel.PlayerNameInputField.text);
		}
		else
		{
			PhotonNetwork.playerName = _menupanel.PlayerNameInputField.text;
			PlayerPrefs.SetString("NamePlayer", _menupanel.PlayerNameInputField.text);
		}
	}
}
